const express = require('express');
const { getSocketByHotelId } = require('../utils');
const localInterface = require('./local-interface');

const router = express.Router();

router.get('/', (req, res, next) => {
  res.send('Welcome to use NCloud Service!');
  next();
});

router.post('/remote-call', async (req, res, next) => {
  console.log('api /remote-call req.body', req.body);

  try {
    const { hotelId } = req.body;
    const socket = await getSocketByHotelId(hotelId);

    socket.emit('call', req.body, data => {
      res.send(data);
    });
  } catch (e) {
    next(e);
  }
});

router.use('/interface', localInterface);

module.exports = router;
